package com.scau.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.scau.pojo.Section;
import com.scau.pojo.Video;
import com.scau.service.SectionService;
import com.scau.service.VideoService;

@RestController
@RequestMapping("/videoController")
public class VideoController{

	@Autowired
	VideoService videoService;
	
	@RequestMapping("/queryAllVideo")
	public List<Video> query(@RequestParam int seId) {
		return videoService.query(seId);
	}
	
	@RequestMapping("/isNameExist")
	public int isExist(@RequestParam String name,@RequestParam Integer seId) throws Exception{
		int result=-1;
		try{
			result=videoService.isNameExist(name,seId);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/isSeqExist")
	public int isExist(@RequestParam Integer vSeq,@RequestParam Integer seId) throws Exception{
		int result=-1;
		try{
			
			result=videoService.isSeqExist(vSeq,seId);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/addVideo")
	public int add(@RequestBody JSONObject jsonObject) throws Exception{
		int result=0;
		try{
			result=videoService.add(jsonObject);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/deleteVideo")
	public int deleteMulti(@RequestBody JSONArray jsonArray) throws Exception{
		int result=0;
		try{
			result=videoService.delete(jsonArray);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/editVideo")
	public int edit(@RequestBody JSONObject jsonObject) throws Exception{
		int result=0;
		try{
			result=videoService.update(jsonObject);
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/getVideoFile")
	public String getPPT(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception{
		System.out.println("test");
		BufferedOutputStream stream=null;
		Map<String,Object> resultMap=new HashMap<>();
		String filePath="E:\\bishe\\SEWebsite\\src\\main\\webapp\\videos\\";
		if(!file.isEmpty()){
			try {
				byte[] bytes=file.getBytes();
				String savePath=filePath+file.getOriginalFilename();
				stream=new BufferedOutputStream(new FileOutputStream(new File(savePath)));
				stream.write(bytes);
				stream.close();
				resultMap.put("result","success");
			} catch (Exception e) {
				resultMap.put("result","failure");
				e.printStackTrace();
				return JSONObject.toJSONString(resultMap).toString();
			}
		}else{
			resultMap.put("result","failure");
			return JSONObject.toJSONString(resultMap).toString();
		}
		return JSONObject.toJSONString(resultMap).toString();
	}

}
